home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Science⁄Math
/
Scientist's Helper src
/
s.helper.3
/
regio.c
< prev
Wrap
C/C++ Source or Header
|
1986-02-06
|
19KB
|
851 lines
#include "all.h"
#include "regtabext.h"
WriteGraph(reply)
SFReply *reply;
{
int f, i, j, k, vsize, hsize;
char str[256];
long count, bytesAvailable, pntg, mpnt, dstBytes;
Finfo info;
char dstbuf[512], srcbuf[512];
char *srcPtr, *dstPtr, *mypointer;
if( !(reply->good) ) {
return;
}
strncpy( &pntg, "PNTG", 4 );
strncpy( &mpnt, "MPNT", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )==noErr ) { /*delete old file, if any*/
if( info.fdType!=pntg ) {
ErrMsg("cant overwrite a non-Paint file");
}
else {
if( FSDelete(reply->fName, reply->vRefNum)!=noErr ) {
ErrMsg("couldnt delete existing file");
}
}
}
bytesAvailable = 0;
for( i=0; i<10; i++ ) {
k=GetVInfo( i, str, &j, &count );
if( (k==noErr) && (j==reply->vRefNum) ) {
bytesAvailable=count;
break;
}
}
/* I really should check here if there is enough space on disk */
/* but I dont really see how to do this without compressing the entire bit map first*/
if( Create(reply->fName, reply->vRefNum, mpnt, pntg)!=noErr ) {
ErrMsg("couldnt create file");
}
if( FSOpen(reply->fName, reply->vRefNum, &f )!=noErr ) {
ErrMsg("couldnt open file");
}
if( SetFPos( f, fsFromStart, 0L )!=noErr ) {
ErrMsg("couldnt find beginning of file");
}
for( i=0; i<512; i++ ) { /*header is block of 512 nulls*/
dstbuf[i]=0;
}
count=512L;
k = FSWrite(f, &count, &dstbuf[0] );
if( (k!=noErr) || (count!=512L) ){
FSClose(f);
ErrMsg("couldnt write graph");
}
mypointer = grMap.baseAddr;
hsize = grMap.rowBytes;
vsize = grMap.bounds.bottom - grMap.bounds.top;
for( j=1; j<=720; j++ ) {
srcPtr = &srcbuf[0];
dstPtr = &dstbuf[0];
for( i=0; i<72; i++ ) {
if( (i<hsize) && (j<=vsize) ) {
srcbuf[i]=*mypointer;
mypointer++;
}
else {
srcbuf[i] = 0;
}
}
PackBits( &srcPtr, &dstPtr,72);
dstBytes = (long)(dstPtr-&dstbuf[0]);
count = dstBytes;
k = FSWrite(f, &count, &dstbuf[0] );
if( (k!=noErr) || (count!=dstBytes) ){
FSClose(f);
ErrMsg("couldnt write graph");
}
} /*end for*/
if( FSClose(f)!=noErr ) {
ErrMsg("couldnt close file");
}
FlushVol( str, reply->vRefNum );
}
ReadProcedure(reply)
SFReply *reply;
{
char str[512];
int i, j, k, f, oldWindow;
long regTabType, count;
Finfo info;
GrafPtr oldPort;
if( !(reply->good) ) {
return;
}
strncpy( (char*)(®TabType), "TEXT", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )!=noErr ) {
ErrMsg("couldnt get file info");
}
if( info.fdType!=regTabType ) {
ErrMsg("not a Text file");
}
/*open file*/
if( FSOpen( reply->fName, reply->vRefNum, &f)!=noErr ) {
ErrMsg("couldnt open file");
}
SetFPos( f, fsFromStart, 0L );
GetPort(&oldPort);
oldWindow=currentWindow;
currentWindow=prWindow;
whichWindow=theWindow[prWindow];
SetPort(whichWindow);
TESetSelect( (long)((*prText)->teLength),(long)((*prText)->teLength),prText);
TEKey( '\r', prText );
TEKey( '\r', prText );
for( i=0; i<63; i++ ) {
count=512L;
k=FSRead(f,&count,str);
if( (k==noErr) || (k==eofErr) ) {
TEInsert( str, count, prText );
}
else {
break;
}
if( count<512L ) {
break;
}
} /*end for*/
FSClose(f);
IfOutScroll(prText);
SetPort( oldPort );
currentWindow=oldWindow;
whichWindow=theWindow[currentWindow];
}
WriteProcedure(reply)
SFReply *reply;
{
int f, i, j, k;
char str[256];
long count, bytesNeeded, bytesAvailable, regTabType;
Finfo info;
if( !(reply->good) ) {
return;
}
strncpy( ®TabType, "TEXT", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )==noErr ) { /*delete old file, if any*/
if( info.fdType!=regTabType ) {
ErrMsg("cant overwrite a non-Text file");
}
else {
if( FSDelete(reply->fName, reply->vRefNum)!=noErr ) {
ErrMsg("couldnt delete existing file");
}
}
}
bytesNeeded = (*prText)->teLength;
bytesAvailable = 0;
for( i=0; i<10; i++ ) {
k=GetVInfo( i, str, &j, &count );
if( (k==noErr) && (j==reply->vRefNum) ) {
bytesAvailable=count;
break;
}
}
if( bytesAvailable<bytesNeeded ) {
ErrMsg( "not enough space on disk" );
}
if( Create(reply->fName, reply->vRefNum, regTabType, regTabType)!=noErr ) {
ErrMsg("couldnt create file");
}
if( FSOpen(reply->fName, reply->vRefNum, &f )!=noErr ) {
ErrMsg("couldnt open file");
}
if( SetFPos( f, fsFromStart, 0L )!=noErr ) {
ErrMsg("couldnt find beginning of file");
}
count=bytesNeeded;
HLock( (*prText)->hText );
k = FSWrite(f, &count, *((*prText)->hText) );
HUnlock( (*prText)->hText );
if( (k!=noErr) || (count!=bytesNeeded) ){
FSClose(f);
ErrMsg("couldnt write procedure");
}
if( FSClose(f)!=noErr ) {
ErrMsg("couldnt close file");
}
FlushVol( str, reply->vRefNum );
}
ReadAsciiTable(reply)
SFReply *reply;
{
char cLast, str[512], s[80];
int i, j, k, f, lines, columns, firstReturn;
long regTabType, count;
Finfo info;
float x;
GrafPtr oldPort;
if( !(reply->good) ) {
return;
}
GetPort(&oldPort);
SetPort( theWindow[edWindow] );
InvalRect( &(theWindow[edWindow]->portRect) );
SetPort( oldPort );
strncpy( (char*)(®TabType), "TEXT", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )!=noErr ) {
ErrMsg("couldnt get file info");
}
if( info.fdType!=regTabType ) {
ErrMsg("not a Text file");
}
/*open file*/
if( FSOpen( reply->fName, reply->vRefNum, &f)!=noErr ) {
ErrMsg("couldnt open file");
}
SetFPos( f, fsFromStart, 0L );
/* determine size of table*/
lines = 0;
columns = 0;
i=0; /*counts tabs per line*/
j=0; /*counts characters*/
cLast='\0';
firstReturn=TRUE;
while( lines<=4097 ) { /*loop over lines in file */
count=1L;
k=FSRead(f,&count,str);
if( (count!=1L) || (k!=noErr) ) {
if( (cLast=='\r') || (!firstReturn) ) {
break;
}
else {
str[0]='\r';
firstReturn=FALSE;
}
}
j++;
cLast=str[0];
if( str[0]=='\t') {
i++;
}
else if( str[0]=='\r' ) {
lines++;
if( i>columns ) {
columns=i;
}
i=0;
} /*end if*/
} /*end while*/
columns++; /*since tabs were counted*/
if( columns>32 ) {
columns=32;
}
lines--; /*since first line is colnames*/
FSClose(f);
/* set up table header */
if( (lines>table.header.rows) || (columns>table.header.cols) ) {
AllocTable( lines, columns );
WriteLine("Warning: table allocation changed");
}
else {
table.header.rows = lines;
table.header.cols = columns;
}
table.header.interpolated = FALSE;
for( i=1; i<=table.header.cols; i++ ) {
strcpy( s, "Col " );
IToS( i, &(s[4]) );
strcpy( table.header.colName[i-1], s );
} /*end for*/
for( i=1; i<=table.header.rows; i++ ) {
for( j=1; j<=table.header.cols; j++ ) {
SetTable(i,j,infinity,FALSE);
}} /*end fors*/
/* now read in the data */
if( FSOpen( reply->fName, reply->vRefNum, &f)!=noErr ) {
ErrMsg("couldnt open file");
}
SetFPos( f, fsFromStart, 0L );
/*read colnames*/
i=1;
s[0]='\0';
while( i<=table.header.cols ) {
count=1L;
k=FSRead(f,&count,str);
if( (count!=1L) || (k!=noErr) ) {
break;
}
str[1]='\0';
if( (str[0]=='\t') || (str[0]=='\r') ) {
strcpy( table.header.colName[i-1], s );
i++;
s[0]='\0';
if( str[0]=='\r' ) {
break;
}
}
else {
strcat( s, str );
}
} /*end while*/
/*read table entries*/
i=1;
s[0]='\0';
cLast='\0';
firstReturn=TRUE;
while( i<=table.header.rows ) {
j=1;
while( j<=table.header.cols ) {
count=1L;
k=FSRead(f,&count,str);
if( (count!=1L) || (k!=noErr) ) {
if( (cLast=='\r') || (!firstReturn) ) {
break;
}
else {
str[0]='\r';
firstReturn=FALSE;
}
}
str[1]='\0';
cLast=str[0];
if( (str[0]=='\t') || (str[0]=='\r') ) {
SToR( s, &x, TRUE );
SetTable(i,j,x,TRUE);
j++;
s[0]='\0';
if( str[0]=='\r' ) {
i++;
break;
}
}
else {
strcat( s, str );
}
} /*end while j*/
} /*end while i*/
FSClose(f);
Header2Vars();
}
WriteAsciiTable(reply)
SFReply *reply;
{
int f, i, j, k;
char str[256];
long count, bytesNeeded, regTabType;
Finfo info;
float x;
if( !(reply->good) ) {
return;
}
strncpy( ®TabType, "TEXT", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )==noErr ) { /*delete old file, if any*/
if( info.fdType!=regTabType ) {
ErrMsg("cant overwrite a non-Text file");
}
else {
if( FSDelete(reply->fName, reply->vRefNum)!=noErr ) {
ErrMsg("couldnt delete existing file");
}
}
}
if( Create(reply->fName, reply->vRefNum, regTabType, regTabType)!=noErr ) {
ErrMsg("couldnt create file");
}
if( FSOpen(reply->fName, reply->vRefNum, &f )!=noErr ) {
ErrMsg("couldnt open file");
}
if( SetFPos( f, fsFromStart, 0L )!=noErr ) {
ErrMsg("couldnt find beginning of file");
}
for( i=1; i<=table.header.cols; i++ ) {
strcpy( str, table.header.colName[i-1] );
if( i<table.header.cols ) {
strcat( str, "\t" );
}
else {
strcat( str, "\r" );
}
bytesNeeded = (long) strlen( str );
count=bytesNeeded;
k = FSWrite(f, &count, str );
if( (k!=noErr) || (count!=bytesNeeded) ){
FSClose(f);
ErrMsg("couldnt write colnames");
}
} /*end for*/
for( i=1; i<=table.header.rows; i++ ) {
for( j=1; j<=table.header.cols; j++ ) {
GetTable(i,j,&x);
RToS( x, str );
if( j<table.header.cols ) {
strcat( str, "\t" );
}
else {
strcat( str, "\r" );
}
bytesNeeded = (long) strlen( str );
count=bytesNeeded;
k = FSWrite(f, &count, str );
if( (k!=noErr) || (count!=bytesNeeded) ){
FSClose(f);
ErrMsg("couldnt write table");
} /*end if*/
} } /*end fors*/
if( FSClose(f)!=noErr ) {
ErrMsg("couldnt close file");
}
FlushVol( str, reply->vRefNum );
}
Mean()
{
int i, colx, coly, stat;
float sum, sum2, x, mean, stdDev, n;
char str1[cmdWordLen], str2[cmdWordLen];
SToI( command.cmdWord[2], &colx );
sum = 0.0;
sum2 = 0.0;
n = 0.0;
i = 1;
while ( NextNotNan(i,colx,colx,&i) ) {
GetTable(i,colx,&x);
sum += x;
sum2 += x*x;
n += 1.0;
i++;
}
mean = sum / n;
stdDev = sqrt( (n*sum2 - sum*sum)/(n*(n-1)) );
strcpy(str2,"mean ");
RToS( mean, str1 );
stat = SetVar("mean",str1);
strcat( str2, str1 );
RToS( stdDev, str1 );
stat = stat && SetVar("stddev",str1);
strcat(str2, " stddev ");
strcat(str2, str1 );
RToS( n, str1 );
stat = stat && SetVar("counts",str1);
strcat( str2, " counts ");
strcat( str2, str1 );
if( strcmp(command.cmdWord[1],"type")==0 ) {
WriteLine(str2);
}
else if( strcmp(command.cmdWord[1],"keep")==0 ) {
SToI( command.cmdWord[3], &coly );
for( i=1; i<=table.header.rows; i++ ) {
SetTable(i,coly,mean,FALSE);
}
}
else if( strcmp(command.cmdWord[1],"remove")==0 ) {
SToI( command.cmdWord[3], &coly );
for( i=1; i<=table.header.rows; i++ ) {
GetTable(i,colx,&x);
SetTable(i,coly,(x-mean),FALSE);
}
}
else if( strcmp(command.cmdWord[1],"compute")==0 ) {
;
}
else {
ErrMsg(badModifier);
}
if( !stat ) {
ErrMsg("couldnt set variables");
}
}
Trend()
{
int i, colx, coly, colz, stat;
float x, y, sumX, sumY, sumX2, sumY2, sumXY, idata;
float s0, intercept, slope, errIntercept, errSlope;
char str1[cmdWordLen], str2[cmdWordLen];
SToI( command.cmdWord[2], &colx );
SToI( command.cmdWord[3], &coly );
sumX = 0.0;
sumY = 0.0;
sumX2 = 0.0;
sumY2 = 0.0;
sumXY = 0.0;
idata = 0.0;
i = 1;
while( NextNotNan(i,colx,coly,&i) ) {
GetTable(i,colx,&x);
GetTable(i,coly,&y);
sumX += x;
sumY += y;
sumX2 += x*x;
sumY2 += y*y;
sumXY += x*y;
idata += 1.0;
i++;
}
slope = (sumXY - (sumX*sumY/idata)) / (sumX2-(sumX*sumX/idata));
intercept = (sumY/idata) - (slope*(sumX/idata));
s0 = sumY2 - (sumY*sumY/idata) - (slope*(sumXY - sumX*(sumY/idata)));
s0 = s0 / (idata-2.0);
errIntercept = sqrt( (double)(s0 * sumX2 / (idata*(sumX2 - (sumX*sumX/idata)))) );
errSlope = sqrt( (double) (s0 / (sumX2 - (sumX*sumX/idata))) );
RToS(slope,str2);
stat = SetVar("slope",str2);
strcpy( str1,"slope ");
strcat( str1, str2 );
RToS( intercept, str2 );
stat = stat && SetVar("intercept",str2);
strcat( str1, " intercept ");
strcat( str1, str2 );
RToS( idata, str2 );
stat = stat && SetVar("counts", str2);
strcat( str1, " counts ");
strcat( str1, str2 );
if( strcmp(command.cmdWord[1],"type")==0 ) {
WriteLine(str1);
}
RToS(errSlope,str2);
stat = stat && SetVar("errslope",str2);
strcpy( str1, "errslope ");
strcat( str1, str2 );
RToS( errIntercept, str2 );
stat = stat&& SetVar("errintercept",str2);
strcat( str1, " errintercept ");
strcat( str1, str2 );
if (strcmp(command.cmdWord[1],"type")==0 ) {
WriteLine(str1);
}
else if (strcmp(command.cmdWord[1],"keep")==0 ) {
SToI( command.cmdWord[4], &colz );
for ( i=1; i<=table.header.rows; i++ ) {
GetTable(i,colx,&x);
SetTable(i,colz,(intercept+(slope*x)),FALSE);
}
}
else if (strcmp(command.cmdWord[1],"remove")==0 ) {
SToI( command.cmdWord[4], &colz );
for ( i=1; i<=table.header.rows; i++ ) {
GetTable(i,colx,&x);
GetTable(i,coly,&y);
SetTable(i,colz,y-(intercept+(slope*x)),FALSE);
}
}
else if (strcmp(command.cmdWord[1],"compute")==0 ) {
;
}
else {
ErrMsg(badModifier);
}
if( !stat ) {
ErrMsg("couldnt set variables");
}
}
SortTable()
{
int sortCol, i, j, k, cmp();
sortRec *s;
long bytesNeeded;
float x;
SToI( command.cmdWord[1], &sortCol );
if (table.header.interpolated) {
ErrMsg("cant sort interpolated table");
}
if( GoodCol(sortCol)!=0 ) {
ErrMsg("column not in table");
}
bytesNeeded = 6L * (3L + (long)(table.header.rows));
if( (s=NewPtr(bytesNeeded)) == 0L ) {
ErrMsg("not enough free memory");
}
for( i=1; i<=table.header.rows; i++ ) {
(s+i)->r = i;
GetTable( i, sortCol, &x );
if( NaN(&x) ) {
(s+i)->v = minfinity; /*nan's sorted to bottom of table*/
}
else {
(s+i)->v = x;
}
} /*end for*/
qsort( (s+1), table.header.rows, 6, cmp );
for( j=1; j<=table.header.cols; j++ ) {
for( i=1; i<=table.header.rows; i++ ) {
GetTable( (s+i)->r, j, &x );
(s+i)->v = x;
}
for( i=1; i<=table.header.rows; i++ ) {
SetTable( i, j, (s+i)->v, FALSE );
}
} /* end for j */
DisposPtr(s);
}
cmp(a,b)
sortRec *a, *b;
{
if( (a->v) < (b->v) ) {
return(-1);
}
else if( (a->v) == (b->v) ) {
return(0);
}
else {
return(1);
}
}
TabInterpolate()
{
int *ix, *ixy; /*ix, iy, and z are temporary arrays created on the heap*/
float *z;
int ndata, npair, col, i, j, k, il, ir, newRows, oldRows;
int first, found;
float x, y, t, u, v, w;
long bytesNeeded;
char str[cmdWordLen];
if( table.header.interpolated ) {
ErrMsg("table already interpolated");
}
if( table.header.rows < 2 ) {
ErrMsg("not enough rows");
}
if( table.header.cols < 2 ) {
ErrMsg("not enough cols");
}
SToR( command.cmdWord[1], &(table.header.samp), FALSE );
if (table.header.samp<=0) {
table.header.samp = 1.0;
ErrMsg("samp must be > 0");
}
bytesNeeded = 2L * (3+table.header.maxRows);
if( (ix=NewPtr(bytesNeeded)) == 0L ) {
ErrMsg("not enough free memory");
}
if( (ixy=NewPtr(bytesNeeded)) == 0L ) {
DisposPtr(ix);
ErrMsg("not enough free memory");
}
bytesNeeded = 4L * (3+table.header.maxRows);
if( (z=NewPtr(bytesNeeded)) == 0L ) {
DisposPtr(ix);
DisposPtr(ixy);
ErrMsg("not enough free memory");
}
ndata = 0;
first = TRUE;
for( i=1; i<=table.header.rows; i++ ) {
GetTable(i,1,&x);
if (!NaN(&x)) {
if( first ) {
ndata++;
*(ix+ndata) = i;
first = FALSE;
} /*end if first*/
else { /*not first*/
ndata++;
*(ix+ndata) = i;
GetTable( *(ix+ndata-1), 1, &y );
if( x<=y ) {
DisposPtr(ix);
DisposPtr(ixy);
DisposPtr(z);
ErrMsg("col 1 does not monotonically increase");
} /*end if x<=y*/
} /*end if not first*/
} /*end if not NaN */
} /*end for*/
if (ndata<2) {
DisposPtr(ix);
DisposPtr(ixy);
DisposPtr(z);
ErrMsg("not enough good x-values");
}
GetTable(*(ix+1),1,&(table.header.start));
GetTable( *(ix+ndata), 1, &t );
x = 1.0 + ((t-table.header.start)/table.header.samp);
oldRows = table.header.rows;
if ( ((long)x) > ((long)table.header.maxRows) ) {
WriteLine("warning. some data lost from end of columns");
newRows = table.header.maxRows;
} /*end if*/
else {
newRows= (int)x;
} /*end if*/
for (col=2; col<=table.header.cols; col++ ) {
npair = 0;
for( i=1; i<=ndata; i++ ) {
GetTable( *(ix+i), col, &x );
if (!NaN(&x)) {
npair++;
*(ixy+npair) = *(ix+i);
} /*end if not NaN*/
} /*end for*/
if (npair<2) {
IToS( col, str );
WritePhrase("warning: not enough data in col ");
WriteLine(str);
table.header.rows = newRows;
for( j=1; j<=table.header.rows; j++ ) {
SetTable(j,col,infinity,FALSE);
}
table.header.rows=oldRows;
} /*end if pairs<2*/
else { /*npairs>=2*/
il = 1;
ir = 2;
for( i=1; i<=newRows; i++ ) {
x = table.header.start + table.header.samp*((float)(i-1));
if( GetTable(*(ixy+il),1,&t) || (x<t) ) {
*(z+i) =infinity;
} /*end if (x<t)*/
else if( GetTable(*(ixy+npair),1,&t) || (x>t) ) {
*(z+i) = infinity;
}
else if( GetTable(*(ixy+il),1,&t) || GetTable(*(ixy+ir),1,&u) || ((x>=t)&&(x<=u)) ) {
GetTable( *(ixy+il), col, &t );
GetTable( *(ixy+ir),col, &u );
GetTable( *(ixy+il), 1, &v );
GetTable( *(ixy+ir), 1, &w );
*(z+i) = t + (((u-t)/(w-v))*(x-v));
}
else { /*search*/
j = il+1;
found = FALSE;
while( (!found) && (j<=(npair-1)) ) {
il = j;
ir = j+1;
GetTable( *(ixy+il), 1, &t );
GetTable( *(ixy+ir), 1, &u );
if( (x>=t) && (x<=u) ) {
found = TRUE;
} /*end in interval*/
j++;
} /*end while*/
GetTable( *(ixy+il), col, &t );
GetTable( *(ixy+ir),col, &u );
GetTable( *(ixy+il), 1, &v );
GetTable( *(ixy+ir), 1, &w );
*(z+i) = t + (((u-t)/(w-v))*(x-v));
} /*end else search*/
} /*end for i*/
table.header.rows = newRows;
for( i=1; i<=table.header.rows; i++ ) {
SetTable( i, col, *(z+i), FALSE );
} /*end for i*/
table.header.rows = oldRows;
} /*end if pairs>=2*/
} /*end for col*/
table.header.interpolated=TRUE;
table.header.rows = newRows;
Header2Vars();
CreateCol1();
DisposPtr(ix);
DisposPtr(ixy);
DisposPtr(z);
}